﻿Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.InteropServices

<System.Runtime.InteropServices.ComVisible(False)>
Public Class ClientChannel

  ''' <summary>
  ''' Die aktuelle Instanz des Cache.
  ''' </summary>
  ''' <remarks></remarks>
  Private Shared myInstance As ClientChannel

  ''' <summary>
  ''' Das aktuelle IObserver-Objekt.
  ''' </summary>
  ''' <remarks></remarks>
  Public Shared Channel As TcpChannel

  Sub New()
    MyBase.New()
  End Sub

  Private Shared registered As Boolean = False

  Public ReadOnly Property Connected As Boolean
    Get
      Return registered
    End Get
  End Property

  Public Sub Attach(ByRef channel As TcpChannel)
    If registered Then ChannelServices.UnregisterChannel(ClientChannel.Channel)
    ClientChannel.Channel = channel
    ChannelServices.RegisterChannel(ClientChannel.Channel, True)
    registered = True
  End Sub

  Public Sub Detach()
    ChannelServices.UnregisterChannel(ClientChannel.Channel)
    ClientChannel.Channel = Nothing
    registered = False
  End Sub

  ''' <summary>
  ''' Holt die aktuelle Instanz oder erstellt eine Neue.
  ''' </summary>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Shared Function GetInstance() As ClientChannel
    If Object.ReferenceEquals(myInstance, Nothing) Then
      myInstance = New ClientChannel()
    End If
    Return myInstance
  End Function






End Class